package com.qilu.pe.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.Article;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.ui.adapter.ArticleAapter;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class ArticleListActivity extends Base2Activity {
    private RecyclerView rcv_article;// 精选文章

    private ArticleAapter articleAapter;// 精选文章
    private List<Article> articleList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_article_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_article = findViewById(R.id.rcv_article);

        articleAapter = new ArticleAapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rcv_article.setLayoutManager(manager);
        rcv_article.setAdapter(articleAapter);

        setHeadTitle("精选文章");

        setListeners();

        hideProgress();
        reqArticleList();
        showProgress();
    }

    private void setListeners() {
        articleAapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("article", articleList.get(position));
                startActivity(ArticleDetailActivity.class, bundle);
            }
        });
    }

    // 请求文章列表
    private Disposable reqArticleList() {
        return APIRetrofit.getDefault().reqArticleList("0", 1, 999999)
                .compose(RxUtil.<BaseResult2<List<Article>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Article>>>() {
                    @Override
                    public void accept(BaseResult2<List<Article>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {

                        } else {

                        }
                        articleList = listBaseResult2.getData();
                        articleAapter.setDataList(articleList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }
}